var Workport = Workport || {};

Workport.MultiSelect = (function(){
	var timer;

	function msChange (_val, _dataItems, _msObj, _eeObj) {
        var _newNames = [], _len, _list = _dataItems, _newIds = "";
        _len = _list.length;
        for (var i = 0; i < _len; i++) {
            _newNames.push(_list[i].text);
            _newIds += _val[i] + ";";
        }
        _newIds = _newIds.substring(0, _newIds.length - 1);
        
        _eeObj.names = _newNames;
        _eeObj.idsStr = _newIds;
    };

    // function msKeyupEvent (_msObj, _names, _showLoaderFunc, _hideLoaderFunc) {
    function msKeyupEvent (_inputElem, _msObj, _names, _showLoaderFunc, _hideLoaderFunc) {
        _msObj.msElem.data("kendoMultiSelect").setDataSource([]);
        _msObj.msElem.data("kendoMultiSelect").close();
        // var val = _msObj.msDiv.find("span").text();
        var val = _inputElem.val();

        clearTimeout(timer);
        if (!$.trim(val)) {
            return;
        }
        
        timer = setTimeout(function () {
                var _e = _msObj.msElem.data("kendoMultiSelect");
                val = $.param({key:val}).substring(4);
                _showLoaderFunc();
                //get data
                Workport.ContactAPI.searchContact(function (data) {
                    var contacts, source = [], len;
                    _hideLoaderFunc();
                    if (data.Contacts) {
                        contacts = data.Contacts;
                        len = contacts.length;
                        for (var i = 0; i < len; i++) {
                            source.push({text: contacts[i].UserName, value: contacts[i].EmailId});
                        }
                        _e.setDataSource(source);
                        _e.open();
                    }
                }, {userId: val, userName: val, location: "", position: "", action: 3, pageSize: 0, offset: 0});
            }, 2000);
    };

    return {
    	createMultiSelect : function (_msObj, _eeObj, _callbackFunc, _showLoaderFunc, _hideLoaderFunc) {
            _msObj.msElem.kendoMultiSelect({
                dataTextField: "text", 
                dataValueField: "value", 
                filter: "contains",
                close: function(e) {
                    _msObj.msDiv.find("div.k-multiselect").addClass("k-state-focused");
                },
                change: function(e) {
                    msChange(this.value(), this.dataItems(), _msObj, _eeObj);
                    _callbackFunc(_eeObj.idsStr);
                    this.setDataSource([]);
                }
            });

            _msObj.msDiv.find(".k-input").css("border", "0px");
            _msObj.msDiv.find("div.k-multiselect-wrap").addClass("focusBorder");
            _msObj.msDiv.find(".k-input").blur(function () {
                _msObj.msDiv.find("div.k-multiselect").removeClass("k-state-focused");
                clearTimeout(timer); });

            var inputElem = _msObj.msDiv.find("input.k-input");
            inputElem.unbind('input').bind('input', function () {
                msKeyupEvent(inputElem, _msObj, _eeObj.names, _showLoaderFunc, _hideLoaderFunc);
            });

            _msObj.msElem.blur(function () {_msObj.msElem.data("kendoMultiSelect").setDataSource([])});
        }
	}
})();